﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using eRechnung_3_0.Properties;
using System.Diagnostics;

namespace eRechnung_3_0 {
    
    
    public partial class DsInvoice {
        partial class TblInvSkontoDataTable
        {

            public void UpdateSkontoTage(TblInvSkontoRow Row)
            {
                decimal tage = -1;
                DateTime Datum;
                TimeSpan ts = new TimeSpan();
//                string strReDatum = eRechnung_3_0.Globals.ThisDocument.CC_InvoiceDate.Text.ToString();

                Datum = eRechnung_3_0.Globals.ThisDocument.Rechnungsdatum;
                
                if (!(Row.IsSpBisDatumNull()))  // Bis Datum ist vorhanden
                {
                    ts = Row.SpBisDatum.Subtract(Datum);
                    tage = Convert.ToDecimal(ts.TotalDays);
                }
                Row.SpTage = tage;
                Debug.WriteLine("Skonto Row changed:"+
                                   Row.SpTage.ToString()+";"+
                                   Row.SpBisDatum.ToShortDateString().ToString()+";"+
                                   Row.SpSkontoProz.ToString("0.00%")+";"+
                                   Row.spBasisBetrag.ToString("0.00")+";"+
                                   Row.spSkontoBetrag.ToString("0.00"));
            }

            public void UpdateSkontoDatum(TblInvSkontoRow Row)
            {
                DateTime Datum;
                //                string strReDatum = eRechnung_3_0.Globals.ThisDocument.CC_InvoiceDate.Text.ToString();

                Datum = eRechnung_3_0.Globals.ThisDocument.Rechnungsdatum;

                Row.SpBisDatum = Datum.AddDays(Convert.ToDouble(Row.SpTage));
                Debug.WriteLine("Skonto Row changed:" +
                                   Row.SpTage.ToString() + ";" +
                                   Row.SpBisDatum.ToShortDateString().ToString() + ";" +
                                   Row.SpSkontoProz.ToString("0.00%") + ";" +
                                   Row.spBasisBetrag.ToString("0.00") + ";" +
                                   Row.spSkontoBetrag.ToString("0.00"));
            }

        }
    
        partial class TblInvDetailsDataTable
        {
        }
    
        partial class TblMwStSummenDataTable
        {
        }

        public void LoadMwStTab()
        {
            string MwStTab = Settings.Default.setMwStTab;
            if (MwStTab!="")
            {
                this.TblMWST.Clear();
                StringReader sr = new StringReader(MwStTab);
                this.TblMWST.ReadXml(sr);
                this.TblInvDetails.SpMwStColumn.DefaultValue = Settings.Default.setMwStDefault;
                eRechnung_3_0.Globals.ThisDocument.SetRibbonButtonsEnable(true);
            }
            else
            {
                this.TblMWST.Clear();
                TblMWSTRow tblRow = (TblMWSTRow) this.TblMWST.NewRow();
                tblRow.SpMwSt = 20;
                tblRow.SpBeschreibung = "Standardsteuersatz";
                this.TblInvDetails.SpMwStColumn.DefaultValue = 20;
                eRechnung_3_0.Globals.ThisDocument.SetRibbonButtonsEnable(false);
            }
        }

        public string ToStringAsXml(DataTable dt)
        {
            StringWriter sw = new StringWriter();
            dt.WriteXml(sw, XmlWriteMode.IgnoreSchema);
            string s = sw.ToString();
            return s;
        }

    }
}
